package com.sidianzhong.sdz.service.impl;
import com.github.pagehelper.PageHelper;
import com.sidianzhong.sdz.mapper.*;
import com.sidianzhong.sdz.model.*;
import com.sidianzhong.sdz.service.*;
import com.sidianzhong.sdz.utils.PageInfo;
import com.sidianzhong.sdz.utils.Tools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* Created by hxgqh on 2016/1/7.
*/
@Service
@Transactional
public class ConstantServiceImpl implements ConstantService{
    protected static final Logger LOG = LoggerFactory.getLogger(ConstantServiceImpl.class);
    // 将所有的modelMapper注入
        @Autowired
    private ConstantMapper constantMapper;

    @Override
    public Constant create(Constant item) {
         constantMapper.insert(item);
         return item;
    }

    @Override
    public int delete(Integer id) {
        return constantMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(Constant item) {
        return constantMapper.updateByPrimaryKeySelective(item);
    }

    @Override
    public Constant get(Integer id) {
        return constantMapper.selectByPrimaryKey(id);
    }
    @Override
    public PageInfo<Constant> getListWithPaging(Integer pageNum, Integer pageSize,
                                                  String sortItem, String sortOrder,Constant item){
       ConstantExample  example = new ConstantExample();
       ConstantExample.Criteria criteria = example.createCriteria();
       if(item.getItemKey() != null ){
            criteria.andItemKeyEqualTo(item.getItemKey());
        }
        if(item.getItemValue() != null ){
            criteria.andItemValueEqualTo(item.getItemValue());
        }
        if(item.getItemDescribe() != null ){
            criteria.andItemDescribeEqualTo(item.getItemDescribe());
        }
       example.setOrderByClause(Tools.humpToLine(sortItem)+" "+sortOrder);
       PageHelper.startPage(pageNum, pageSize);
       List<Constant> list = this.constantMapper.selectByExample(example);
       PageInfo result = new PageInfo(list);
       return result;
    }

    @Override
    public List<Constant> getList(Integer pageNum, Integer pageSize,
                                                      String sortItem, String sortOrder,Constant item){
       ConstantExample  example = new ConstantExample();
       ConstantExample.Criteria criteria = example.createCriteria();
       if(item.getItemKey() != null ){
            criteria.andItemKeyEqualTo(item.getItemKey());
        }
        if(item.getItemValue() != null ){
            criteria.andItemValueEqualTo(item.getItemValue());
        }
        if(item.getItemDescribe() != null ){
            criteria.andItemDescribeEqualTo(item.getItemDescribe());
        }
       example.setOrderByClause(Tools.humpToLine(sortItem)+" "+sortOrder);
       PageHelper.startPage(pageNum, pageSize);
       List<Constant> list = this.constantMapper.selectByExample(example);
       return list;
    }

     @Override
     public PageInfo<JSONObject> getListWithObject(Integer pageNum, Integer pageSize,
                                                      String sortItem, String sortOrder,Constant item){
        ConstantExample  example = new ConstantExample();
        ConstantExample.Criteria criteria = example.createCriteria();
        if(item.getItemKey() != null ){
            criteria.andItemKeyEqualTo(item.getItemKey());
        }
        if(item.getItemValue() != null ){
            criteria.andItemValueEqualTo(item.getItemValue());
        }
        if(item.getItemDescribe() != null ){
            criteria.andItemDescribeEqualTo(item.getItemDescribe());
        }
        example.setOrderByClause(Tools.humpToLine(sortItem)+" "+sortOrder);
        PageHelper.startPage(pageNum, pageSize);
        List<Constant> list = this.constantMapper.selectByExample(example);
        List<JSONObject> lists =  list.stream().map(constant -> JSONObject.parseObject(JSONObject.toJSONString(constant))).collect(Collectors.toList());
        PageInfo result = new PageInfo(lists);
        return result;
        }

    @Override
    public String selectValueByKey(String Key) {

//        ConstantExample  example = new ConstantExample();
//        ConstantExample.Criteria criteria = example.createCriteria();
//        criteria.andItemKeyEqualTo(Key);
//        List< Constant> list = this.constantMapper.selectByExample(example);
//        Constant constant;
//        try {
//            constant = list.get(0);
//        } catch (Exception e) {
//            return null;
//        }
//        return constant.getItemValue();\

        Constant constant = this.constantMapper.selectByKey(Key);
        if (constant!=null){
            return constant.getItemValue();
        }

        return null;
    }

}
